package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCarService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void addShoppingCar(ShoppingCartDTO shoppingCartDTO) {
        //1.先查询购物车表格是否有数据
        //找到指定用户得指定购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝属性,把DTO转换为entity
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //用户数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //指定用户ID，指定菜品ID或者套餐id得购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        //2.存在，数量加1
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            shoppingCart = shoppingCartList.get(0);
            int number = shoppingCart.getNumber() + 1;
            shoppingCart.setNumber(number);
            //更新数据到购物车表
            shoppingCartMapper.updateNumber(shoppingCart);
        } else {
            //3.不存在，将数据补充完整，插入到表中
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                //菜
                Dish dish = dishMapper.getById(dishId);
                //补充冗余字段信息：图片、菜品、价格
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                //获取指定id套餐得所有信息
                Setmeal setmeal = setmealMapper.getById(setmealId);
                //补充冗余字段信息：图片、菜品、价格
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());

            }

            //补充字段
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());


            //插入到表中
            shoppingCartMapper.insert(shoppingCart);


        }
    }


    //查看购物车
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //构建shoppingCart对象，设置userId当做查询得条件
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }


    //清空购物车
    @Override
    public void cleanShoppingCart() {
        //根据当前用户id进行删除购物车
        /*
        因为当进行到这个接口的时候就说明已经登录过了，用户登录过的
            id会存在于线程的局部变量中，所以通过线程的局部变量可以拿到
            用户的id，然后根据用户的id进行清空购物车的操作
        * */
        Long userId = BaseContext.getCurrentId();

        shoppingCartMapper.deleteByUserId(userId);
    }


    //删除单个商品
    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
        //1.先拿到该账号的id
                //创建一个购物车的对象
        ShoppingCart shoppingCart = new ShoppingCart();
            //拷贝赋值
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            //通过自定义的BaseContext类拿到id
        Long userId = BaseContext.getCurrentId();
            //给购物车对象的id赋值
        shoppingCart.setUserId(userId);
        //查询该用户下的购物车里面的数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
                //拿到购物车里面的数据
              shoppingCart = shoppingCartList.get(0);

        //拿到购物车里面商品的数量
        Integer number = shoppingCart.getNumber();
        if (number == 1){
            //如果商品的数量等于1的话，就直接根据id删除商品
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }else if (number>1){
            //如果商品的数量大于1的话，我们就去修改里面的数据，把商品数量减一
            number=number-1;
            shoppingCart.setNumber(number);
            shoppingCartMapper.updateNumber(shoppingCart);
        }







        Dish dishId = dishMapper.getById(shoppingCart.getDishId());


    }
}
